package service

import (
	"admin100/app/model"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"context"
)

var Session = &SessionService{}

type SessionService struct {
}

const SessionKey = "ns_session_key"

func (s *SessionService) SetUser(ctx context.Context, ctxUser *model.MyadminUser) {
	customCtx := Context.Get(ctx)
	if customCtx != nil {
		customCtx.Session.Set(SessionKey, ctxUser)
	}
}

func (s *SessionService) GetUser(ctx context.Context) *model.MyadminUser {
	customCtx := Context.Get(ctx)
	if customCtx != nil {
		if v := Context.Get(ctx).Session.GetVar(SessionKey); !v.IsNil() {
			var user = &model.MyadminUser{}
			if err := v.Struct(user); err != nil {
				defErr.Panic(define.Fail)
			}
			return user
		}
	}

	return nil
}

func (s *SessionService) DelUser(ctx context.Context) {
	customCtx := Context.Get(ctx)
	if customCtx != nil {
		if err := customCtx.Session.Remove(SessionKey); err != nil {
			defErr.Panic(define.Fail)
		}
	}

}
